package Models;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author Tanja
 */
public class SalesCollection
{
    private ArrayList<Sale> sales;
    private static SalesCollection instance = new SalesCollection();

    private SalesCollection()
    {
        this.sales = new ArrayList<Sale>();
    }

    public static SalesCollection getInstance()
    {
        return SalesCollection.instance;
    }

    public boolean addSale(Sale newSale)
    {
       return this.sales.add(newSale);
    }

    public ArrayList<Sale> findSale(Double totalMin, Double totalMax, Date startDate, Date endDate, Customer customer, Employee employee, Boolean delivery, Boolean delivered, Boolean paid)
    {
         ArrayList<Sale> foundSale = new ArrayList<Sale>();

        for(Sale sale : this.sales)
        {
			if(sale.isActivated())
			{
				boolean found = false;
				if(sale. getTotal() > totalMin && sale.getTotal() < totalMax)
				{ found = true; }
				else if(sale.getDate() != null)
				{
					if(startDate != null && sale.getDate().after(startDate))
					{ found = true; }
					if(endDate != null && sale.getDate().before(endDate))
					{ found = true; }
				}
				else if(sale.getCustomer().equals(customer))
				{ found = true; }
				else if(sale.getEmployee().equals(employee))
				{ found = true; }
				else if(sale.isDelivery() == delivery)
				{ found = true; }
				else if(sale.isDelivered() == delivered)
				{ found = true; }
				else if(sale.isPaid() == paid)
				{ found = true;}

				if(found == true)
				{
					foundSale.add(sale);
				}
			}
        }
        return foundSale;
       
    }

    public Sale getSale(int id)
    {
       for(Sale sale : sales)
        {
            if(sale.getId() == id)
            {
                return sale;
            }
        }
        return null;
    }

	public ArrayList<Sale> getAllSales()
	{
		ArrayList<Sale> activatedSales = new ArrayList<Sale>();
		for (Sale sale : this.sales)
		{
			if(sale.isActivated())
			{
				activatedSales.add(sale);
			}
		}
		return activatedSales;
	}

    public void removeSale(Sale sale)
    {
        sale.setActivated(false);
    }
}
